<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *会员列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
);

//会员列表
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid'],'status'=>4);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(!empty($_RQ['id'])){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	//创建时间
	if($_RQ['dimissiontime']){
		$time_range=cfc('tools')->base64_decode($_RQ['dimissiontime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['dimissiontime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['dimissiontime']=$time_range;
	}
	$list=pdo_getslice('jy_scrm_worker',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=array('nickname'=>$item['name'],'avatar'=>$item['avatar'],'mobile'=>$item['nickname']);
			//$item['kehunum']=pdo_count('jy_scrm_worker_dimission',array('handover_userid'=>$item['userid'],'uniacid'=>$_SESSION['uniacid']));
			//$item['lastkehunum']=pdo_count('jy_scrm_worker_dimission',array('status'=>0,'handover_userid'=>$item['userid'],'uniacid'=>$_SESSION['uniacid']));
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：从通讯录将离职员工删除后，可以分配他的客户及客户群给其他员工继续跟进，且客户及客户群信息也一并给接替的员工。','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'member','title'=>'离职成员','type'=>'member'),
			array('name'=>'kehunum','title'=>'持有客户数','type'=>'string'),
			array('name'=>'qunnum','title'=>'持有群聊数','type'=>'string'),
			array('name'=>'lastkehunum','title'=>'待分配客户数','type'=>'string'),
			array('name'=>'lastqunnum','title'=>'待分配群聊数','type'=>'string'),
			array('name'=>'dimissiontime','title'=>'离职时间 ','type'=>'time'),
		),'batch'=>array(
			array('type'=>'post','url'=>'jy_scrm/worker.dimission/upworker/','title'=>'同步离职成员'),
		)),
		'search'=>array(
			array('name'=>'name','title'=>'员工名称','type'=>'string','width'=>'15'),
		),	
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_scrm_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$item['department']=$item['department']?json_decode($postdata['department'],true):array();
	$item['external_corp_name']=$item['external_corp_name']?json_decode($postdata['external_corp_name'],true):array();
	$item['extattr']=$item['extattr']?json_decode($postdata['extattr'],true):array();
	$return['item']=$item;
	$return['item']['createtime']=date('Y-m-d H:i:s',$item['createtime']);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回成员列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'成员名称','name'=>'name','help'=>''),
					array('type'=>'string','title'=>'成员别名','name'=>'alias','help'=>''),
					array('type'=>'string','title'=>'座机','name'=>'telephone','help'=>''),
					array('type'=>'string','title'=>'手机号码','name'=>'mobile','help'=>''),
					array('type'=>'string','title'=>'邮箱','name'=>'email','help'=>'长度6~64个字节，且为有效的email格式。企业内必须唯一，手机号码/邮箱二者不能同时为空'),
					array('type'=>'string','title'=>'职务信息','name'=>'position','help'=>''),
					array('type'=>'string','title'=>'对外职务','name'=>'external_position','help'=>''),
					array('type'=>'radio','title'=>'性别','name'=>'gender','options'=>'sex','help'=>''),
					array('type'=>'image','title'=>'头像','name'=>'avatar','help'=>''),
					array('type'=>'string','title'=>'地址','name'=>'address','help'=>''),
					array('type'=>'select','title'=>'主部门','name'=>'main_department','options'=>'groups','help'=>'主部门'),
					
				)
			),
				array(
				'title'=>'部门设置',
				'list'=>array(
						array('type'=>'array','name'=>'department','add_tip'=>'添加一个部门',
								'list'=>array(
									array('type'=>'select','options'=>'groups','name'=>'id','title'=>'部门'),	
									array('type'=>'string','name'=>'order','title'=>'排序值'),
									array('type'=>'switch','name'=>'is_leader_in_dept','title'=>'是否上级'),
								),
								'details'=>array(
									array('type'=>'select','options'=>'groups','name'=>'id','title'=>'部门'),	
									array('type'=>'string','name'=>'order','title'=>'排序值'),
									array('type'=>'switch','name'=>'is_leader_in_dept','title'=>'是否上级'),
								)
						),
				)),
				array(
				'title'=>'成员对外属性',
				'list'=>array(
							array('type'=>'string','name'=>'external_corp_name','title'=>'企业对外简称'),
							array(
								'type'=>'array',
								'name'=>'external_profile',
								'add_tip'=>'添加一个字段',
								'list'=>array(
									array('type'=>'string','name'=>'title','title'=>'字段名称'),	
									array('type'=>'radio','name'=>'type','title'=>'字段类型','options'=>'types'),	
								//	array('type'=>'string','name'=>'paixu','title'=>'排序'),	
									array('type'=>'string','name'=>'value','title'=>'选项信息'),	
								),
								'details'=>array(
									array('type'=>'string','name'=>'title','title'=>'字段名称'),	
									array('type'=>'select','name'=>'type','title'=>'字段类型','options'=>'filed_type','option_title'=>'type'),
								//	array('type'=>'string','name'=>'paixu','title'=>'排序','help'=>'从小到大顺序排序'),
									array('type'=>'string','name'=>'value','title'=>'选项信息','help'=>'单选和多选设置此条信息，选项之间以英文分号;隔开，例如:选项1;选项2;选项3'),
								)
							),	
				)),
				array(
				'title'=>'自定义表单',
				'list'=>array(
							array(
								'type'=>'array',
								'name'=>'extattr',
								'add_tip'=>'添加一个字段',
								'list'=>array(
									array('type'=>'string','name'=>'title','title'=>'字段名称'),	
									array('type'=>'radio','name'=>'type','title'=>'字段类型','options'=>'types'),	
								//	array('type'=>'string','name'=>'paixu','title'=>'排序'),	
									array('type'=>'string','name'=>'value','title'=>'选项信息'),	
								),
								'details'=>array(
									array('type'=>'string','name'=>'title','title'=>'字段名称'),	
									array('type'=>'select','name'=>'type','title'=>'字段类型','options'=>'filed_type','option_title'=>'type'),
								//	array('type'=>'string','name'=>'paixu','title'=>'排序','help'=>'从小到大顺序排序'),
									array('type'=>'string','name'=>'value','title'=>'选项信息','help'=>'单选和多选设置此条信息，选项之间以英文分号;隔开，例如:选项1;选项2;选项3'),
								)
							),	
				)
			),
		)
	);	
	$return['groups']=pdo_getall('jy_scrm_worker_group',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']));
	$return['sex']=array(1=>'男',2=>'女');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'name'=>trim($postdata['name']),
		'alias'=>trim($postdata['alias']),
		'telephone'=>trim($postdata['telephone']),
		'mobile'=>trim($postdata['mobile']),
		'email'=>trim($postdata['email']),
		'position'=>trim($postdata['position']),
		'external_position'=>trim($postdata['external_position']),
		'avatar'=>trim($postdata['avatar']),
		'address'=>trim($postdata['address']),
		'gender'=>intval($postdata['gender']),
		'main_department'=>intval($postdata['main_department']),
		'department'=>ijson_encode($postdata['department']),
		'extattr'=>ijson_encode($postdata['extattr']),
		'external_profile'=>ijson_encode($postdata['external_profile']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_scrm_worker',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$_SESSION['onemerchid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_scrm_worker',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_scrm_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择会员！','error');
	}
	$post=get_postdata();
	if(isset($post['isblack'])){
		pdo_update('jy_scrm_worker',array('isblack'=>intval($post['isblack'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='upworker'){
	$workers=pdo_getall('jy_scrm_worker',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','userid'));
	if(count($workers)<=0){
		exi('请先同步通讯录！','error');
	}
	$workers=$this->model('worker')->get_dimissionworker();
	$this->model('model')->check_apilog('get_dimissionworker_members',true);
	$this->model('model')->add_apilog('get_dimissionworker_members');
	exi('同步成员成功！');
}